package com.example.monitorapp.viewmodel

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.ResponseData
import com.example.baselib.databean.UserData
import com.example.monitorapp.UserInfoRepository

/**
 * Author: sxygsj
 * Date: 2020/4/21 9:03
 * Description: 用户管理
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class UserInfoViewModel(application: Application) :BaseViewModel(application){
    private val userData:MutableLiveData<UserData> by lazy { MutableLiveData<UserData>() }
    private val repository= UserInfoRepository()
    private lateinit var result:MutableLiveData<ResponseData<String>>
    /**
     * 通过livedata 监听user信息变化
     */
    fun getUserData():LiveData<UserData>{
        userData.value=MyConstants.userData
        return userData
    }

    /**
     * 返回结果
     */
    fun getResult():LiveData<ResponseData<String>>{
        result=MutableLiveData<ResponseData<String>>()
        return result
    }

    fun getVerificationCode(tel: String) =launchUI{
        val result=repository.getBindVerCode(tel,MyConstants.userData.token)
        Toast.makeText(getApplication<Application>(),result.body()?.msg, Toast.LENGTH_SHORT).show()
    }

    fun bindingTel(tel: String, vercode: String)=launchUI{
       val result=repository.bindTel(tel,vercode,MyConstants.userData.token)
        this@UserInfoViewModel.result.value=result.body()
    }

    fun changePwd(oldPwd: String, newPwd: String, surePwd: String) =launchUI{
        val result=repository.changePwd(oldPwd,newPwd,surePwd,MyConstants.userData.token)
        this@UserInfoViewModel.result.value=result.body()
    }
}